#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int x=0;bool f=0;char ch=getchar();
	while(ch<'0'||ch>'9')f^=(ch=='-'),ch=getchar();
	while('0'<=ch&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
int e[1005][1005];
int vis[1005];
int ans;
int n,m;
void dfs(int x,int cnt,int res){
	if(cnt==n){
		ans=min(ans,res);return;
	}
	if(cnt<n-1){
		for(int i=2;i<=n;i++)
			if(!vis[i]){
				vis[i]=1;
				dfs(i,cnt+1,res+e[x][i]);
				vis[i]=0;
			}
	}else vis[1]=1,dfs(1,n,res+e[x][1]),vis[1]=0;
}
int main(){
	freopen("circ.in","r",stdin);
	freopen("circ.out","w",stdout);
	int T=read();
	while(T--){
		n=read();m=read();
		ans=n;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				e[i][j]=1;
		int ok=0;
		for(int i=1;i<=m;i++){
			int u=read(),v=read();
			if(u==v)ok=1;
			e[u+1][v+1]=0;
		}
		dfs(1,0,0);
		if(ok)ans=n-1;
		printf("%d\n",ans);
	}
	return 0;
}
/*
1
5 4
4 1
1 3
4 2
0 3

4
3 4
0 1
1 2
2 0
0 1
3 5
0 0
1 2
2 0
0 1
0 1
4 2
0 1
2 3
6 6
0 2
1 5
3 0
4 0
1 4
5 0
*/
